package com.wxr.config;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName UserRealm
 * @Description TODO
 * @Author wangxinrui
 * @Date 2023/9/23 12:45
 * @Version
 **/
@Configuration
public class UserRealm extends AuthorizingRealm {

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行了授权");
        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        //获取当前用户 , 正常是应该从数据库中获取，目前直接写死的
        String name = "root";
        String password = "123456";
        UsernamePasswordToken userToken = (UsernamePasswordToken)authenticationToken;
        if(!userToken.getUsername().equals(name)){
           return null;
        }
        //密码认证，shiro做
        System.out.println("执行了认证");
        return new SimpleAuthenticationInfo("",password,"");
    }
}
